﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using DSS_XNA.Engine.Actor;
using DSS_XNA.Engine.Objects;

namespace DSS_XNA.Engine.HUD.Controls
{
    public class DevInfo : IHUDControl
    {
        #region Properties

        public StringBuilder Builder
        {
            get { return _builder; }
            set { _builder = value; }
        }
        private StringBuilder _builder = new StringBuilder();

        public int NumberOfLines
        {
            get { return _numberOfLines; }
            set { _numberOfLines = value; }
        }
        private int _numberOfLines = 0;

        public bool ShallRender
        {
            get { return _shallRender; }
            set { _shallRender = value; }
        }
        private bool _shallRender = true;

        #endregion

        #region IHUDControl Members

        public void Render(SpriteBatch spriteBatch, ref Vector2 relativePosition, SpriteFont font)
        {
            // don't render to the cient screen if we have the developer window open
            if (!this.ShallRender)
                return;

            spriteBatch.DrawString(font, this.Builder.ToString(), relativePosition, Color.Black);

            relativePosition.Y += font.LineSpacing * this.NumberOfLines;
        }

        public void Run(GameTime gameTime, Game1 engine)
        {
            // clear out the old text
            this.Builder = new StringBuilder();
            this.NumberOfLines = 0;

            if (engine.CurrentWorld != null)
            {
                this.Builder.AppendLine(String.Format("World Offset: {0}, {1} Size in Tiles: {2}, {3}", engine.CurrentWorld.CurrentWorld.GetProperty("OffsetX"),
                    engine.CurrentWorld.CurrentWorld.GetProperty("OffsetY"),
                    engine.CurrentWorld.CurrentWorld.GetProperty("WidthInTiles"),
                    engine.CurrentWorld.CurrentWorld.GetProperty("HeightInTiles")));
                this.NumberOfLines++;

                if (engine.CurrentWorld.CurrentWorld.FocusedEntity != null)
                {
                    this.Builder.AppendLine(String.Format("World Focused Entity: {0}", engine.CurrentWorld.CurrentWorld.FocusedEntity.Name));
                    this.NumberOfLines++;
                }
                else
                {
                    this.Builder.AppendLine("World Focused Entity: None");
                    this.NumberOfLines++;
                }
            }

            foreach (IActor a in engine.CurrentWorld.CurrentWorld.Actors)
            {
                this.Builder.AppendLine(String.Format("Name: {0}  Pos: {1}, {2}  Accel: {3}, {4}", a.Name, a.Position.X, a.Position.Y, a.Acceleration.X, a.Acceleration.Y));
                this.NumberOfLines++;
            }

            foreach (DSS_XNA.Engine.Objects.Object o in engine.CurrentWorld.CurrentWorld.Objects)
            {
                this.Builder.AppendLine(String.Format("Id: {0} Name: {1}", o.Id, o.Name));
                this.NumberOfLines++;
                //Builder.AppendLine(String.Format("Pos: {0},{1} Points: {2}", o.Position.X, o.Position.Y, ((BonusObject)o).Points));
                this.NumberOfLines++;
            }

            this.Builder.AppendLine(String.Format("Number of Expired Objects: {0}", engine.CurrentWorld.CurrentWorld.ExpiredObjects.Count));
            this.NumberOfLines++;

            this.Builder.AppendLine(String.Format("Object Count: {0}", engine.CurrentWorld.CurrentWorld.Objects.Count));
            this.NumberOfLines++;

            // add this to the developer window if it is showing
            if (engine.DeveloperWindow != null)
            {
                engine.DeveloperWindow.SetDeveloperInfo(this.Builder.ToString());
                this.ShallRender = false;
            }
            else
            {
                this.ShallRender = true;
            }
        }

        #endregion
    }
}
